package com.cnbot.dchttpsdk.base;


import android.content.Context;

import rx.Subscription;
import rx.subscriptions.CompositeSubscription;


/**
 * Copyright (c) 2016-a  Hunan Cnbot Co., Ltd. All Rights Reserved.
 *
 * @descriptoin MVP中model层的基类,封装了增加订阅和注销订阅的函数
 * @FileName: BaseModel.java
 * @author: dc
 * @date: 2019/1/18 18:02
 * @version: 1.0
 */
public class BaseModel {

    private CompositeSubscription mCompositeSubscription;
    protected Context mContext;

    public BaseModel(Context context) {
        mContext = context;
    }



    protected void addSubscribe(Subscription subscription) {
        if (mCompositeSubscription == null) {
            mCompositeSubscription = new CompositeSubscription();
        }
        mCompositeSubscription.add(subscription);
    }

    /**
     * 注销，防止内存泄漏
     */
    public void unSubscribe() {

        if (mCompositeSubscription != null && mCompositeSubscription.hasSubscriptions()) {
            mCompositeSubscription.clear();
        }

        if (mContext != null) {
            mContext = null;
        }
    }

}